Plotting Your Data

GVPT399F: Power, Politics, and Data

Data visualisation

We will use data visualization to answer the following question:

Do cars with big engines use more fuel than cars with small engines?

Set up your plot

An empty canvas!

ggplot(data = mpg)

Map your aesthetics

ggplot(data = mpg, mapping = aes(x = displ, y = hwy))

Add in your cars

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point()

EXERCISE

  1. Make a scatter plot of hwy vs cyl.


  1. What happens if you make a scatter plot of class vs drv? Why is the plot not useful?


  1. Why does the following give an error and how would you fix it?
ggplot(data = mpg) + 
  geom_point()

ANSWERS

  1. Make a scatter plot of hwy vs cyl.
ggplot(mpg, mapping = aes(x = hwy, y = cyl)) + 
  geom_point()

ANSWERS

  1. What happens if you make a scatter plot of class vs drv? Why is the plot not useful?
ggplot(mpg, mapping = aes(x = class, y = drv)) + 
  geom_point()

ANSWERS

  1. Why does the following give an error and how would you fix it?
ggplot(data = mpg) + 
  geom_point()

ANSWERS

  1. Why does the following give an error and how would you fix it?
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point()